# @Author: dpf
# @Date  : 2024/12/10/10:25
import sys

from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import QWidget, QApplication, QMessageBox

from dao import userDao
from entity.UserModel import User


class Ui_Form(QWidget):

    def __init__(self):
        super(Ui_Form, self).__init__()
        self.setWindowFlag(QtCore.Qt.WindowType.MSWindowsFixedSizeDialogHint)
        self.setupUi(self)

    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.formLayoutWidget = QtWidgets.QWidget(parent=Form)
        self.formLayoutWidget.setGeometry(QtCore.QRect(60, 40, 291, 171))
        self.formLayoutWidget.setObjectName("formLayoutWidget")
        self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
        self.formLayout.setContentsMargins(0, 0, 0, 0)
        self.formLayout.setHorizontalSpacing(20)
        self.formLayout.setVerticalSpacing(30)
        self.formLayout.setObjectName("formLayout")
        self.label = QtWidgets.QLabel(parent=self.formLayoutWidget)
        self.label.setObjectName("label")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label)
        self.oldPasswordInput = QtWidgets.QLineEdit(parent=self.formLayoutWidget)
        self.oldPasswordInput.setObjectName("oldPasswordInput")
        self.oldPasswordInput.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password)
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.ItemRole.FieldRole, self.oldPasswordInput)
        self.newPasswordInput = QtWidgets.QLineEdit(parent=self.formLayoutWidget)
        self.newPasswordInput.setObjectName("newPasswordInput")
        self.newPasswordInput.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password)
        self.formLayout.setWidget(2, QtWidgets.QFormLayout.ItemRole.FieldRole, self.newPasswordInput)
        self.label_2 = QtWidgets.QLabel(parent=self.formLayoutWidget)
        self.label_2.setObjectName("label_2")
        self.formLayout.setWidget(2, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label_2)
        self.label_3 = QtWidgets.QLabel(parent=self.formLayoutWidget)
        self.label_3.setObjectName("label_3")
        self.formLayout.setWidget(3, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label_3)
        self.newPasswordInput2 = QtWidgets.QLineEdit(parent=self.formLayoutWidget)
        self.newPasswordInput2.setObjectName("newPasswordInput2")
        self.newPasswordInput2.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password)
        self.formLayout.setWidget(3, QtWidgets.QFormLayout.ItemRole.FieldRole, self.newPasswordInput2)
        self.label_4 = QtWidgets.QLabel(parent=self.formLayoutWidget)
        self.label_4.setObjectName("label_4")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label_4)
        self.userNameLabel = QtWidgets.QLabel(parent=self.formLayoutWidget)
        self.userNameLabel.setText("")
        self.userNameLabel.setObjectName("userNameLabel")
        self.userNameLabel.setText(userDao.currentUser.logName)
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.ItemRole.FieldRole, self.userNameLabel)
        self.pushButton = QtWidgets.QPushButton(parent=Form)
        self.pushButton.setGeometry(QtCore.QRect(150, 240, 75, 30))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("../images/editPassword.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
        self.pushButton.setIcon(icon)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.modifyPassword)
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "密码修改"))
        self.label.setText(_translate("Form", "原密码"))
        self.label_2.setText(_translate("Form", "新密码"))
        self.label_3.setText(_translate("Form", "确认新密码"))
        self.label_4.setText(_translate("Form", "用户名"))
        self.pushButton.setText(_translate("Form", "提交"))

    def modifyPassword(self):
        old_password = self.oldPasswordInput.text()
        if old_password.strip() == '':
            QMessageBox.warning(None, '系统提示', '请输入原密码')
            return
        if old_password != userDao.currentUser.password:
            QMessageBox.warning(None, '系统提示', '原密码输入不正确')
            return
        new_password = self.newPasswordInput.text()
        if new_password.strip() == '':
            QMessageBox.warning(None, '系统提示', '请输入新密码')
            return
        new_password2 = self.newPasswordInput2.text()
        if new_password2 != new_password:
            QMessageBox.warning(None, '系统提示', '确认新密码输入不正确')
            return
        user = User.modify_password_constructor(self.userNameLabel.text(), old_password, new_password)
        if userDao.modifyPassword(user) > 0:
            QMessageBox.information(None, '系统提示', '密码修改成功！')
            self.resetForm()
        else:
            QMessageBox.warning(None, '系统提示', '密码修改失败！')

    def resetForm(self):
        self.oldPasswordInput.clear()
        self.newPasswordInput.clear()
        self.newPasswordInput2.clear()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = Ui_Form()
    ui.show()
    sys.exit(app.exec())
